﻿Imports System.Data.SqlClient
Public Class FrmThongKeBanDoTheoTyLeCapTinh
    Private Sub NapDMTyLe()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTyLe", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxTyle.DisplayMember = "TenTyLe"
        ComboBoxTyle.ValueMember = "MaTyle"
        ComboBoxTyle.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub DataGridViewBanDo_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewBanDo.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewBanDo.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewBanDo.RowHeadersWidth < Size.Width + 20 Then DataGridViewBanDo.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
    Private Sub NapDataGridViewTraBanDo()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select " &
                               "   dc.ToBanDoSo " &
                                " , dc.TongThua " &
                                " , dc.TongDienTich " &
                                "  ,tl.TenTyLe " &
                                "  ,dc.NamThanhLap " &
                                " , dc.DonViDo " &
                                " , ht.TenHienTrang " &
                                "  ,dc.DiaDanh " &
                                "  ,ke.tenke " &
                                "  ,k.TenKho " &
                                " , tx.tenxa " &
                                " , th.T_huyen " &
                                " From  BanDoDiaChinh dc, DMTyLe tl, DMHienTrang ht, DMKho k, DMKe ke, tenxa tx, tenhuyen th" &
                                " WHERE dc.tyleid = tl.matyle " &
                                " and dc.MaHienTrangID = ht.MaHienTrang " &
                                " and dc.TyleID= '" & ComboBoxTyle.SelectedValue.ToString & "'" &
                                " and dc.makhoid = k.makhoid " &
                                " and dc.maKeid = ke.makeid " &
                                " and dc.mahuyenid = th.maH " &
                                " and dc.maxaid = tx.maxa ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewBanDo.DataSource = tbl
        If (tbl.Rows.Count = 0) Then
            MessageBox.Show("Không tìm thấy thông tin !")
        Else
        End If
        ob_cnn.Close()
    End Sub
    Private Sub XuatExcel()
        ' xuat truc tiep ra excel
        Dim gr As SaveFileDialog = New SaveFileDialog()
        Dim FileName As String
        With gr
            .InitialDirectory = "D:\"
            .Filter = "Microsoft Excel Worksheet (*.xls)|*.xls"
            .FilterIndex = 1
        End With
        If gr.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            FileName = gr.FileName
        Else
            Exit Sub
        End If
        'Extracting from database
        Dim col, row As Integer
        Dim Excel As Object = CreateObject("Excel.Application")

        If Excel Is Nothing Then
            MsgBox("m***cel re..", MsgBoxStyle.Critical)
            Return
        End If
        'Export to Excel process
        Try
            With Excel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add()
                .Worksheets(1).Select()
                .Worksheets(1).name = "Ban do theo ty le "
                Dim i As Integer = 1
                For col = 0 To DataGridViewBanDo.Columns.Count - 1
                    .cells(1, i).value = DataGridViewBanDo.Columns(col).HeaderText
                    .cells(1, i).EntireRow.Font.Bold = True
                    i += 1
                Next
                i = 2
                Dim k As Integer = 1
                For col = 0 To DataGridViewBanDo.Columns.Count - 1
                    i = 2
                    For row = 0 To DataGridViewBanDo.Rows.Count - 1
                        With .worksheets(1)
                            .Cells(i, k).Value = DataGridViewBanDo.Rows(row).Cells(col).Value
                            i += 1
                        End With
                    Next
                    k += 1
                Next
                .ActiveCell.Worksheet.SaveAs(FileName)
            End With
            System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
            Excel = Nothing
            MsgBox("Dữ liệu xuất ra Excel thành công!", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ' The excel is created and opened for insert value. We most close this excel using this system
        Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
        For Each i As Process In pro
            i.Kill()
        Next
    End Sub

    Private Sub FrmThongKeBanDoTheoTyLe_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        NapDMTyLe()
        DataGridViewBanDo.RowsDefaultCellStyle.BackColor = Color.LightBlue
        DataGridViewBanDo.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
    End Sub

    Private Sub ButtonTimKiem_Click(sender As Object, e As EventArgs) Handles ButtonTimKiem.Click
        NapDataGridViewTraBanDo()
    End Sub

    Private Sub ButtonExcel_Click(sender As Object, e As EventArgs) Handles ButtonExcel.Click
        XuatExcel()
    End Sub

    Private Sub ButtonThoat_Click(sender As Object, e As EventArgs) Handles ButtonThoat.Click
        Me.Close()

    End Sub
End Class